#! /bin/bash

SVANSPROGRAM_HOME="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
LOG_FILE=$SVANSPROGRAM_HOME/install.log

log() {
	echo "$1" >> $SVANSPROGRAM_HOME/install.log
}

log "`date`"
log "Svansprogram directory: ${SVANSPROGRAM_HOME}"

#######################################
# Create executable file              #
#######################################
EXECUTABLE="$SVANSPROGRAM_HOME"/svansprogram.sh
log "Creating shell script: $EXECUTABLE"
cat <<MESSAGEDELIMITER > $EXECUTABLE
java -jar $SVANSPROGRAM_HOME/svansprogram.jar
MESSAGEDELIMITER

log "Setting execute permission"
chmod +x "$EXECUTABLE"

#######################################
# Create desktop file                 #
#######################################
INSTALL_DIR=~/.local/share/applications
log "Install directory: ${INSTALL_DIR}"


if [ ! -d "$INSTALL_DIR" ]; then
	log "Install directory does not exist"
	exit 1
fi

log "Creating desktop file"
cat <<MESSAGEDELIMITER > "$SVANSPROGRAM_HOME"/svansprogram.desktop
#!/usr/bin/env xdg-open

[Desktop Entry]
Version=3.0
Type=Application
Terminal=false
Exec=$EXECUTABLE
Name=Svansprogram
Comment=A Multi tail application
Icon=$SVANSPROGRAM_HOME/icon_128x128.png
Categories=Development
MESSAGEDELIMITER

log "Moving desktop file"
mv "$SVANSPROGRAM_HOME"/svansprogram.desktop "$INSTALL_DIR"
